angular.module("dashboardApp")
.controller('testCtrl', function($scope, testProcess) {

	$scope.result = "optA";

	setTimeout(function() {
		$scope.$apply(function() {
			$scope.$parent.loadingView = false;
		})
	},1000);

	testProcess.getCat().then(function(data) {
		$scope.categories = data;
	})

	// =====================================================
	$scope.addQuestion = function() {
		if(typeof $scope.category == 'undefined') {
			getAlert('Vui lòng chọn Danh mục trước');
			return;
		}
		var data = {};
		data.category = $scope.category;
		data.question = $scope.question;
		data.optA = $scope.answerA;
		data.optB = $scope.answerB;
		data.optC = $scope.answerC;
		data.optD = $scope.answerD;
		data.result = $scope.result;

		testProcess.addQuestion(data).then(function(msg) {
			if(msg == 'success') {
				getAlert('Thêm thành công');
				$scope.question = '';
				$scope.answerA = '';
				$scope.answerB = '';
				$scope.answerC = '';
				$scope.answerD = '';
			}
			else getAlert('Thêm thất bại');
		})
	}
})
.factory('testProcess', function($http, BASE_URL) {
	return {
		getCat: function() {
			return $http.post(BASE_URL + 'dashboard_api/category/getSection')
			.then(function(promise) {
				console.log(promise.data);
				return promise.data;
			})
		},
		addQuestion: function(qst) {
			return $http({
				url: BASE_URL + 'dashboard_api/question/addOptQuestion',
				method: "POST",
				data: $.param({data: qst}),
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			})
			.then(function(promise) {
				console.log(promise.data);
				return promise.data;
			})
		}

	}
})